package com.cqfae.deploycentral.app.utils;

import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@Component
public class TimeUtil {

    // 获得本周一与当前日期相差的天数
    public int getMondayPlus() {
        Calendar cd = Calendar.getInstance();
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1) {
            return -6;
        } else {
            return 2 - dayOfWeek;
        }
    }

    public int getMondayPlus(int days) {
        Calendar cd = Calendar.getInstance();
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1) {
            return -6+days;
        } else {
            return 2 - dayOfWeek+days;
        }
    }

    // 获得当前周-周一的日期
    public  String getCurrentMonday() {
        int mondayPlus = getMondayPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus);
        Date monday = currentDate.getTime();
        return ConvertUtil.convertDSTS(monday);
    }


    // 获得当前周- 周日的日期
    public String getPreviousSunday() {
        int mondayPlus = getMondayPlus();
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.add(GregorianCalendar.DATE, mondayPlus +6);
        Date monday = currentDate.getTime();
        return ConvertUtil.convertDSTS(monday);
    }

    //获得当前周一周的时间
    public List<String> getCurrentWeek() {
        int mondayPlus = getMondayPlus();
        List<String> currentWeekList = new CopyOnWriteArrayList<>();
        for(int i= 0;i<7;i++){
            GregorianCalendar currentDate = new GregorianCalendar();
            currentDate.add(GregorianCalendar.DATE, mondayPlus+i);
            Date monday = currentDate.getTime();
            String preMonday = ConvertUtil.convertDJTS(monday);
            currentWeekList.add(preMonday);
        }
        return currentWeekList;
    }
}
